﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Inspired.Euler
{
    public static class Problem050
    {
        /// <summary>
        /// Which prime, below one-million, can be written as the sum of the most consecutive primes?
        /// </summary>
        [EulerProblem(50, Title = "Which prime, below one-million, can be written as the sum of the most consecutive primes?")]
        public static long Solve()
        {
            int prime = 0;
            int count = 0;
            int max = 1000000;
            int[] primes = Extensions.Primes(max).ToArray();
            for (int skip = 0; skip < primes.Length; skip++)
            {
                Console.Title = (skip / (double)primes.Length).ToString("0.00%") + ";" + prime.ToString();
                int candidate = 0;
                for (int take = 1; (skip + take -1) < primes.Length; take++)
                {
                    candidate = primes.Skip(skip).Take(take).Sum();
                    if (candidate >= max)
                        break;

                    if (candidate.IsPrime() && count < take)
                    {
                        prime = candidate;
                        count = take;
                    }
                }
            }
            return prime;
        }
    }
}
